package tr.edu.ege.communicator;

import java.io.Serializable;
import java.net.InetAddress;
import java.net.UnknownHostException;

import tr.edu.ege.communicator.util.CommunicatorLogger;

/**
 * Holds the address - portNumber pair for the message.
 * 
 * @author Onur ULUSU
 * 
 */
public class Address implements Serializable {

	private static final long serialVersionUID = -1297227469481327826L;

	/**
	 * IP address of the destination of the message.
	 */
	private InetAddress address;

	/**
	 * Port number of the destination of the message.
	 */
	private int portNumber;

	/**
	 * Creates a new {@link Address} instance with the given address and
	 * portNumber pair.
	 * 
	 * @param address
	 *            IP address of the destination of the {@link Message}.
	 * @param portNumber
	 *            Port number of the destination of the {@link Message}
	 */
	public Address(String address, int portNumber) {
		super();
		try {
			this.address = InetAddress.getByName(address);
		} catch (UnknownHostException e) {
			CommunicatorLogger.log("%d is an unknown host..", address);
			e.printStackTrace();
		}
		this.portNumber = portNumber;
	}

	/**
	 * @return the address
	 */
	public InetAddress getAddress() {
		return address;
	}

	/**
	 * @return the portNumber
	 */
	public int getPortNumber() {
		return portNumber;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return address.toString() + ":" + portNumber;
	}
}
